<?php

namespace App\Http\Requests\Admin\Store;

use App\Http\Requests\ApiRequest;
use App\Http\Requests\Concerns\ProductRule;
use App\Models\Panx\Category;
use App\Models\Panx\ProductUrlType;
use App\Services\Utils\Guard;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\UploadedFile;
use Illuminate\Validation\Rule;

/**
 * @property string name
 * @property string title
 * @property string description
 * @property string category_id
 * @property string brand_id
 * @property string asset_id
 * @property string product_mod_id
 * @property string sell_price
 * @property string market_price
 * @property string spu_code
 * @property string stock
 * @property string priority
 * @property array product_tag_ids
 * @property UploadedFile thumb
 * @property UploadedFile|null color_thumb
 * @property array attribute_values
 * @property string product_url_type_id
 * @property string product_url
 * @property string is_deleted
 * @property string is_sale
 * @property array body_position_ids
 * @property string cosmetic_material_id
 * @property string brand_series_id
 */
class ProductStoreRequest extends ApiRequest
{
    use ProductRule;

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $customer_id = Guard::store()->customer_id;

        $rule = [
            'name'                => 'required|string|max:'.self::MAX_STRING,
            'title'               => 'required|string|max:'.self::MAX_STRING,
            'description'         => 'nullable|string|max:'.self::MAX_STRING,
            'category_id'         => 'required|int|max:'.self::MAX_INT,
            'brand_id'            => 'required|int|max:'.self::MAX_INT,
            'sell_price'          => 'required|numeric|max:1000000',
            'market_price'        => 'nullable|numeric|max:1000000',
            // 编码不用限制只能写英文数字， 模型组需要写中文
            'spu_code'            => [
                'required', 'string', 'max:'.self::MAX_STRING, $this->ruleCustomerUnique($customer_id)
            ],
            'stock'               => 'nullable|int|min:0|max:'.self::MAX_INT,
            'content'             => 'nullable|string',
            'priority'            => 'nullable|int|between:0,10000',
        ];

        $thumb_rule = $this->thumbStoreRules();
        $url_rule = $this->urlRules();
        $category_rule = $this->categoryRelateRules($this->category_id);

        return $thumb_rule + $url_rule + $category_rule + $rule;
    }
}
